package posecco.shared;

import java.util.ArrayList;

import posecco.client.FieldService;
import posecco.client.FieldServiceAsync;
import posecco.client.Role;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class FieldRow {

	private final FieldServiceAsync fieldService = GWT.create(FieldService.class);
	private String name;
	private String type;
	private Label errorLabel;
	private Label fieldLabel;
	private Widget field;
	
	public FieldRow(String name, String type) {
		
		if (type.equals("Input")) {
			TextBox myField = new TextBox();
			myField.setName(name);
			myField.getElement().setId(name);
			field = (Widget) myField;
		}
		if (type.equals("PwdInput")) {
			PasswordTextBox myField = new PasswordTextBox();
			myField.setName(name);
			myField.getElement().setId(name);
			field = (Widget) myField;
		}
		if (type.equals("RoleSelect")) {
			final ListBox myField = new ListBox();
			myField.setName(name);
			myField.getElement().setId(name);
			fieldService.roleFieldServer(new AsyncCallback<ArrayList<Role>>() {
						public void onFailure(Throwable caught) {
						}
		
						public void onSuccess(ArrayList<Role> result) {
							
							for (Role role : result) {
								
								myField.addItem(role.getName());
							}
						}
					});
			field = (Widget) myField;
		}
		this.name = name;
		String labelText = Util.getMessage(name + "Label");
		this.type = type;
		fieldLabel = new Label(labelText);
		errorLabel = new Label();
		errorLabel.addStyleName("serverResponseLabelError");
		errorLabel.getElement().setId(name + "ErrorLabel");
	}

	public Label getErrorLabel() {
		return errorLabel;
	}

	public void setErrorLabel(Label errorLabel) {
		this.errorLabel = errorLabel;
	}

	public Label getFieldLabel() {
		return fieldLabel;
	}

	public void setFieldLabel(Label fieldLabel) {
		this.fieldLabel = fieldLabel;
	}

	public void setFieldValue(String fieldValue) {
		((TextBox)this.field).setValue(fieldValue);
	}

	public Widget getField() {
		return field;
	}

	public void setField(Widget field) {
		this.field = field;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
